import React, { useState, useEffect } from "react";
import Taro from "@tarojs/taro";
import { View } from "@tarojs/components";
import { Button, Dialog } from "@nutui/nutui-react-taro";

import { wechatBindPhone, getUserInfo } from "../../../services/account";
import { wxLogin, getWxCode } from "../../../utils/wxApi";

import { showErrorToast } from "../../../utils/util";

import "./index.scss";

function BindPhone(props) {
  const { show, onClose, onCall } = props;

  const [isCheck, setIsCheck] = useState(false);

  // 绑定手机号
  const postBindPhone = (code) => {
    wechatBindPhone({ code }).then((resData) => {
      // console.log(`bindRes===>>>:${JSON.stringify(resData)}`);
      postUserInfo();
      Taro.switchTab({
        url: "/pages/home/home",
      });
      onClose();
    });
  };

  // 获取-用户信息
  const postUserInfo = async () => {
    const res = await getUserInfo();
    Taro.setStorageSync("userInfo", res.data);
  };

  const postLogin = () => {
    wxLogin()
      .then((res) => {
        const { status, token } = res;

        Taro.setStorageSync("token", token);
        getUserInfo();
        Taro.switchTab({
          url: "/pages/home/home",
        });
      })
      .catch(() => {
        // setGlobalData('hasLogin', false)
        // showErrorToast('微信登录失败');
        // Taro.switchTab({
        //   url: '/pages/index/index'
        // })
      });
  };

  return (
    <Dialog
      title="请先绑定手机号"
      visible={show}
      onClose={onClose}
      hideConfirmButton={true}
      hideCancelButton={true}
      className="g_bind_modal"
    >
      <View className="m_bind">
        <Button
          className="btn_bind"
          type="primary"
          openType="getPhoneNumber"
          onGetPhoneNumber={(e) => {
            // console.log(`回调成功====>>>${JSON.stringify(e.detail)}`)
            const { code } = e.detail;
            postBindPhone(code);
          }}
        >
          去绑定
        </Button>
        <Button
          className="btn_close"
          onClick={() => {
            onClose();
          }}
        >
          关闭
        </Button>
      </View>
    </Dialog>
  );
}

export default BindPhone;
